package com.railway.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.railway.bean.User;
import com.railway.service.UserService;
import com.railway.service.impl.UserServiceImpl;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		
		
		String regex = "";
		Pattern pattern = null;
		Matcher matcher = null;
		User user = new User();
		
		Map<String,String[]> map = req.getParameterMap();
		Set<String> keys = map.keySet();
		for (String key:keys) {
			if("user_name".equals(key) && "".equals(map.get(key)[0])) {
				regex = "[A-Za-z]\\w{5,29}";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}else{
					user.setUser_name(map.get(key)[0]);
				}
			}
			if("user_password".equals(key) && "".equals(map.get(key)[0])) {
				regex = "^[a-zA-Z]\\w{5,17}";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			
			if("user_id_type".equals(key) && "".equals(map.get(key)[0])) {
				regex = "/d{1}";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			
			if("user_real_name".equals(key) && "".equals(map.get(key)[0])) {
				regex = "[A-Za-z]\\w{5,29}";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}

			
			if("user_id_num".equals(key) && "".equals(map.get(key)[0])) {
				regex = "(^\\d{15}$)|(^\\d{17}([0-9]|X)$";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			
			if("passenger_type".equals(key) && "".equals(map.get(key)[0])) {
				regex = "/d{1}";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			
			if("user_email".equals(key) && "".equals(map.get(key)[0])) {
				regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			
			
			if("user_phone".equals(key) && "".equals(map.get(key)[0])) {
				regex = "^(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\\d{8}$";
				if(isMatch(pattern, matcher, regex, map.get(key)[0]) == false) {
					resp.sendRedirect("regester.jsp");
					return;
				}
			}
			regex = "";
		}
		String user_name = req.getParameter("user_name");
		String user_password = req.getParameter("user_password");
		int user_id_type = Integer.parseInt(req.getParameter("user_id_type"));
		String user_real_name = req.getParameter("user_real_name");
		String user_id_num = req.getParameter("user_id_num");
		int passenger_type = Integer.parseInt(req.getParameter("passenger_type"));
		String user_email = req.getParameter("user_email");
		String user_phone = req.getParameter("user_phone");
		Date register_time = new Date();
		User u = new User(user_name, user_phone, user_password, user_real_name, user_id_type, user_id_num, user_email, passenger_type,register_time);
		UserService us = new UserServiceImpl();
		int num = us.regester(u);
		if(num == 1) {
			System.out.println("注册成功");
			resp.sendRedirect("login.jsp");
			return;
		}else if(num == -1){
			System.out.println("用户名存在，无法注册");
			resp.sendRedirect("register.jsp");
			return;
		}else if(num == -2){
			System.out.println("email存在，无法注册");
			resp.sendRedirect("register.jsp");
			return;
		}else if(num == -3){
			System.out.println("电话号码存在，无法注册");
			resp.sendRedirect("register.jsp");
			return;
		}else{
			System.out.println("return 0");
		}
		
	}
	
	public boolean isMatch(Pattern pattern,Matcher matcher,String regex,String parameter) {
		pattern = Pattern.compile(regex);
		matcher = pattern.matcher(parameter);
		if(matcher.find()) {
			matcher = matcher.reset();
			return true;
		}else{
			matcher = matcher.reset();
			return false;
		}
		
	}
}
